package com.hmy.gateway.config;

import com.hmy.gateway.common.ribbon.RibbonOkHttpProperties;
import okhttp3.ConnectionPool;
import org.springframework.cloud.commons.httpclient.OkHttpClientConnectionPoolFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OkHttpRibbonConfig {

    @Bean
    public ConnectionPool okHttpClientConnectionPool(OkHttpClientConnectionPoolFactory connectionPoolFactory,
                                                     RibbonOkHttpProperties ribbonOkHttpProperties) {
        return connectionPoolFactory.create(
                ribbonOkHttpProperties.getMaxIdleConnections(),
                ribbonOkHttpProperties.getKeepAliveDuration(),
                ribbonOkHttpProperties.getTimeUnit()
        );
    }

//    /**
//     * https://github.com/openzipkin/brave/tree/master/instrumentation/okhttp3
//     */
//    @Bean
//    public OkHttpClient okHttpClient(ConnectionPool connectionPool, HttpTracing httpTracing) {
//        return new OkHttpClient.Builder()
//                .dispatcher(new Dispatcher(
//                        httpTracing.tracing().currentTraceContext()
//                                .executorService(new Dispatcher().executorService())
//                ))
//                .addNetworkInterceptor(TracingInterceptor.create(httpTracing))
//                .connectionPool(connectionPool)
//                .build();
//    }

}
